package it.com.Test.a2_Serializable;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestOfThenRun {

    static ExecutorService executor = Executors.newFixedThreadPool(3);

    /*
    run 线程计算结果不传递
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main....start....");
        CompletableFuture.runAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
//             int i = 10 / 0;
            System.out.println("异步任务睡眠3s，模拟一号任务");
             try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}
            System.out.println("运行结果：" + i);
        }, executor).
                thenRun(() -> {
            System.out.println("异步任务睡眠3s，模拟二号任务");
            try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}
        });
        System.out.println("main....end....");
/**
 异步任务睡眠3s，模拟一号任务
 main....end....
 运行结果：5
 异步任务睡眠3s，模拟二号任务
 ---------------------------------------
 thenRun(() -> {
 System.out.println("异步任务睡眠3s，模拟二号任务");
 try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}
 });

 加Async之后，二号任务就会以异步的方式运行

 thenRunAsync(() -> {
 System.out.println("异步任务睡眠3s，模拟二号任务");
 try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}
 }, executor);
 System.out.println("main....end....");
  */

    }
}
